package com.itheima.health.task;

import com.itheima.health.mapper.SetMealMapper;
import com.itheima.health.utils.QiniuUtils;
import com.itheima.health.utils.TrashRemovalUtil;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Set;

@Component
public class CleanJob {

    @Autowired
    private StringRedisTemplate srt;

    @Autowired
    private SetMealMapper setMealMapper;

    @Autowired
    private QiniuUtils qiniuUtils;

    @XxlJob("cleanJob")
    public void cleanJob(){
        Set<String> set = setMealMapper.selectImageName();
        SetOperations<String, String> opsForSet = srt.opsForSet();

        set.forEach(x-> opsForSet.add("useImage", TrashRemovalUtil.parse(x)));

        Set<String> difference = opsForSet.difference("allImage", "useImage");
        difference.forEach(x -> qiniuUtils.deleteFileFromQiniu(x));

        srt.delete("useImage");
        srt.delete("allImage");
    }
}
